package org.angelica.common.filter;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 过滤器辅助类
 * @author aizhimin
 */
public class FilterHelper {

    /**
     * api 接口匹配
     */
    private static Pattern API_PATTERN = Pattern.compile("/api.*");
    /**
     * 判断是否为接口请求
     * @param requestUri
     * @return true 是api接口  false 不是api接口
     */
    public static boolean isApiRequest(String requestUri){
        Matcher matcher = API_PATTERN.matcher(requestUri);
        if (matcher.matches()) {
            return true;
        }else{
            return false;
        }
    }

    private static Pattern API_BUILD_REQUEST_HEADER = Pattern.compile("/api/common/buildRequestHeader");
    private static Pattern API_ADMIN_USER = Pattern.compile("/api/account/accUser.*");
    private static Pattern API_ADMIN_ROLE = Pattern.compile("/api/account/accRole.*");
    private static Pattern API_ADMIN_PERMISSION = Pattern.compile("/api/account/accPermission.*");
    /**
     * 不进行签名的接口请求
     * @return
     */
    public static List<Pattern> excludeNoSignApi(){
        List<Pattern> excludeUris = Lists.newArrayList();
        excludeUris.add(API_BUILD_REQUEST_HEADER);
        //临时处理
        excludeUris.add(API_ADMIN_USER);
        excludeUris.add(API_ADMIN_ROLE);
        excludeUris.add(API_ADMIN_PERMISSION);
        return excludeUris;
    }

    /**
     * 需要进行简单签名的接口：只针对时间戳和随机数签名
     * @return
     */
    public static List<Pattern> excludeSimpleSignApi(){
        List<Pattern> excludeUris = Lists.newArrayList();
        return excludeUris;
    }

    private static Pattern API_INIT = Pattern.compile("/api/common/init");
    private static Pattern API_LOGIN = Pattern.compile("/api/login");
    /**
     * 不需要登录的接口请求
     * @return
     */
    public static List<Pattern> excludeNoLoginApi(){
        List<Pattern> excludeUris = Lists.newArrayList();
        //构建请求头
        excludeUris.add(API_BUILD_REQUEST_HEADER);
        //初始化接口
        excludeUris.add(API_INIT);
        //登录接口
        excludeUris.add(API_LOGIN);
        //临时处理
        excludeUris.add(API_ADMIN_USER);
        excludeUris.add(API_ADMIN_ROLE);
        excludeUris.add(API_ADMIN_PERMISSION);
        return excludeUris;
    }

}
